import 'package:earclean/app/config/colors.dart';
import 'package:earclean/app/modules/chat/views/receive_msg_box.dart';
import 'package:earclean/app/modules/chat/views/send_msg_box.dart';
import 'package:earclean/app/modules/widget/app_bar_widget.dart';
import 'package:earclean/app/modules/widget/login_button.dart';
import 'package:earclean/app/modules/widget/login_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../controllers/chat_controller.dart';

class ChatView extends GetView<ChatController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const AppBarWidget(
          backgroundColor: ColorConfig.white, titleStr: '客服小二'),
      body: Column(
        children: [
          Flexible(
            child: Obx(() => ListView.builder(
                  padding: const EdgeInsets.all(8.0),
                  itemBuilder: (_, index) {
                    return cell(index);
                  },
                  itemCount: controller.messages.length,
                )),
          ),
          const Divider(height: 1.0),
          SafeArea(
            child: Container(
              decoration: BoxDecoration(color: Theme.of(context).cardColor),
              child: _buildTextComposer(),
            ),
          ),
        ],
      ),
    );
  }

  Widget cell(index) {
    bool aa = controller.messages[index]['type'] == 2;
    ReceiveMsgBox bb = ReceiveMsgBox(
      message: controller.messages[index]['msg'],
      // avatar: controller.userInfo['user_avatar'] ?? "",
      avatar: 'assets/images/kefu.png',
      time: controller.messages[index]['time'] ?? "",
    );
    SendMsgBox cc = SendMsgBox(
      message: controller.messages[index]['msg'],
      // avatar: 'assets/images/组 35@2x.png',
      avatar: "assets/images/组 35@2x.png",
      time: controller.messages[index]['time'] ?? "",
    );
    return Container(child: aa ? bb : cc);
  }

  Widget _buildTextComposer() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
      color: Colors.white,
      child: Row(
        children: [
          Flexible(
            child: Container(
              decoration: BoxDecoration(
                color: Colors.grey[100],
                // border: Border.all(color: ColorConfig.app_main, width: .5),
                borderRadius: BorderRadius.all(Radius.circular(10.r)),
              ),
              child: LoginInput(
                controller: controller.textController,
                hint: "请输入文字",
                autofocus: true,
                keyBoardType: TextInputType.text,
                onChanged: (text) {
                  controller.text.value = text;
                  controller.checkInput();
                },
              ),
            ),
          ),
          Container(
            width: 80,
            margin: EdgeInsets.symmetric(horizontal: 12.w),
            child: Obx(() => LoginButton(
                  "发送",
                  h: 45,
                  enable: controller.sendEnable.value,
                  onPressed: controller.send,
                )),
          )
        ],
      ),
    );
  }
}
